package com.thunbu.thunbu_meeting_system.utils

import com.thunbu.thunbu_meeting_system.bean.AssetTypeModel
import com.thunbu.thunbu_meeting_system.bean.DepartmentModel
import com.thunbu.thunbu_meeting_system.view.treeView.NodeModel
import com.thunbu.thunbu_meeting_system.view.treeView.TreeNode

object TreeViewUtils {

    /**
     * @Description: 解析树形结构,需要全部,把2级节点当1级节点
     * @author: Yang.Yang33
     * @Date: 2023/4/6 17:43
     */
    fun loadDataNeedAll(type:Int,nodes:MutableList<TreeNode<*>>){
        if(type == 1){
            val assetType = AppUtils.getAssetType()
            assetType?.let {
                if(!it.childList.isNullOrEmpty()){
                    //有一级分类则处理数据
                    nodes.add(TreeNode(AssetTypeModel(id = -1,categoryCode = null,categoryName = "全部",childList = null)))
                    it.childList.forEach {child->
                        val treeNode = TreeNode(child)
                        addChild(treeNode,child.childList)
                        nodes.add(treeNode)
                    }

                }
            }
        }
    }

    /**
     * @Description: 解析树形结构
     * type: 1资产分类 2组织和用户 3组织 4存放地点
     * @author: Yang.Yang33
     * @Date: 2023/4/6 17:43
     */
    fun loadData(type:Int,nodes:MutableList<TreeNode<*>>){
        var list:List<*>? = when(type){
            1 -> mutableListOf(AppUtils.getAssetType())
            2 -> mutableListOf(AppUtils.getDepartmentUserInfo())
            3 -> mutableListOf(AppUtils.getDepartmentInfo())
            4 -> AppUtils.getAddressInfo()
            else -> null
        }
        if(!list.isNullOrEmpty() && list[0] !=null){
            list.forEach {
                val treeNode = TreeNode(it as NodeModel)
                addChild(treeNode, it.child)
                nodes.add(treeNode)
            }
        }
    }

    private fun addChild(parentNode:TreeNode<*>, child:List<NodeModel>?){
        if(!child.isNullOrEmpty()){
            child.forEach {
                val treeNode = TreeNode(it)
                addChild(treeNode,it.child)
                parentNode.addChild(treeNode)
                if(it is DepartmentModel && !it.ypdUserPos.isNullOrEmpty()){
                    //有人员,则把人员挂在下面
                    it.ypdUserPos.forEach { userInfo ->
                        treeNode.addChild(TreeNode(DepartmentModel(id = userInfo.id,agencyCode = null,agencyName = userInfo.realName,null,null,isUser = true,departmentName = it.agencyName)))
                    }
                }
            }
        }
    }
}